﻿using GPM;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LF3.Controllers;
using LF3.Moduls;
using CP.Utility;
using DevExpress.XtraGrid.Views.Grid;

namespace LF3Proj
{
    public partial class FrmCurrentMaterialConsume : BaseForm
    {
        private string HeatID;
        private long PlanID;
        private string MatName;
        private string MatID;
        private string Bulkno;
        public FrmCurrentMaterialConsume()
        {
            InitializeComponent();
        }

        public FrmCurrentMaterialConsume(string heatid, long planid)
            : this()
        {
            this.HeatID = heatid;
            this.PlanID = planid;
        }

        private void FrmCurrentMaterialConsume_Load(object sender, EventArgs e)
        {
            ShowMaterialConsume();
            Lf_Material_ConsumeController mc = new Lf_Material_ConsumeController();
            this.lupMatName.Properties.DataSource = mc.GetMatInfo();
        }

        private void ShowMaterialConsume()
        {
            Lf_Material_ConsumeController mc = new Lf_Material_ConsumeController();
            this.gcMat.DataSource = mc.GetMatConsume(this.HeatID);
            this.gcWS.DataSource = mc.GetWSConsume(this.HeatID);

        }

        private void gvMat_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            Lf_Material_Consume mat = this.gvMat.GetFocusedRow() as Lf_Material_Consume;
            if (null == mat)
                return;
            this.lupMatName.EditValue = mat.materialcode;
            this.MatName = mat.materialdesc;
            this.MatID = mat.materialcode;
            this.Bulkno = mat.bunkerno;
            this.spinCount.EditValue = mat.materialquantity;
        }

        private void gvWS_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            Lf_Material_Consume mat = this.gvWS.GetFocusedRow() as Lf_Material_Consume;
            if (null == mat)
                return;
            this.lupMatName.EditValue = mat.materialcode;
            this.MatName = mat.materialdesc;
            this.MatID = mat.materialcode;
            this.spinCount.EditValue = mat.materialquantity;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (lupMatName.EditValue == null || string.IsNullOrEmpty(lupMatName.EditValue.ToString()))
            {
                ExMessageBox.Show("物料名称不能为空", AppSetter.SoftName);
                return;
            }
            if (this.spinCount.EditValue == null || Convert.ToInt32(spinCount.EditValue) <= 0)
            {
                ExMessageBox.Show("数量必须大于0", AppSetter.SoftName);
                return;
            }
            Lf_Material_Consume mat = new Lf_Material_Consume();
            mat.aggregateno = 5;
            mat.aggregatetype = "4";
            mat.bofschedleid = this.PlanID;
            mat.heatid = this.HeatID;
            mat.materialcode = lupMatName.EditValue.ToString();
            mat.materialdesc = lupMatName.GetColumnValue("materialdesc").ToString();
            mat.materialquantity = Convert.ToInt32(this.spinCount.EditValue);
            mat.bunkerno = "99";
            try
            {
                LF_CurrentHeatController ctrl = new LF_CurrentHeatController();
                ctrl.AddHeatMatConsume(mat);
                ExMessageBox.Show("添加物料消耗成功", AppSetter.SoftName);
                LogHelper.Log("炉次当前投料画面", "新增", "新增物料", this.HeatID, mat.materialcode);
                ShowMaterialConsume();
            }
            catch (Exception ex)
            {
                ExMessageBox.Show(ex.Message, AppSetter.SoftName);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.Bulkno != "99")
            {
                ExMessageBox.Show("该物料是自动采集的不能删除，请联系计算机管理员", AppSetter.SoftName);
                return;
            }
            if (ExMessageBox.Confirm("确定要删除物料名称[" + this.MatName + "]的记录吗？", AppSetter.SoftName) != DialogResult.Yes)
                return;
            if (string.IsNullOrEmpty(this.MatID))
            {
                ExMessageBox.Show("没有选中记录，不能删除", AppSetter.SoftName);
                return;
            }
            if (string.IsNullOrEmpty(this.HeatID))
            {
                ExMessageBox.Show("炉次号为空，不能删除", AppSetter.SoftName);
                return;
            }

            Lf_Material_Consume mat = new Lf_Material_Consume();
            mat.aggregateno = 5;
            mat.aggregatetype = "4";
            mat.bofschedleid = this.PlanID;
            mat.heatid = this.HeatID;
            mat.materialcode = this.MatID;
            mat.bunkerno = this.Bulkno;
            
            try
            {
                LF_CurrentHeatController ctrl = new LF_CurrentHeatController();
                ctrl.DeleteHeatMatConsume(mat);
                ExMessageBox.Show("删除物料消耗成功", AppSetter.SoftName);
                LogHelper.Log("炉次当前投料画面", "删除", "删除物料", this.HeatID, this.MatID);
                ShowMaterialConsume();
            }
            catch (Exception ex)
            {
                ExMessageBox.Show(ex.Message, AppSetter.SoftName);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (lupMatName.EditValue == null || string.IsNullOrEmpty(lupMatName.EditValue.ToString()))
            {
                ExMessageBox.Show("物料名称不能为空", AppSetter.SoftName);
                return;
            }
            if (this.spinCount.EditValue == null || Convert.ToInt32(spinCount.EditValue) <= 0)
            {
                ExMessageBox.Show("数量必须大于0", AppSetter.SoftName);
                return;
            }
            Lf_Material_Consume mat = new Lf_Material_Consume();
            mat.aggregateno = 5;
            mat.aggregatetype = "4";
            mat.bofschedleid = this.PlanID;
            mat.heatid = this.HeatID;
            mat.materialcode = lupMatName.EditValue.ToString();
            mat.materialquantity = Convert.ToInt32(this.spinCount.EditValue);
            mat.bunkerno = "99";
            try
            {
                LF_CurrentHeatController ctrl = new LF_CurrentHeatController();
                ctrl.UpdateHeatMatConsume(mat);
                ExMessageBox.Show("保存物料消耗成功", AppSetter.SoftName);
                LogHelper.Log("炉次当前投料画面", "修改", "修改物料", this.HeatID, this.MatID);
                ShowMaterialConsume();
            }
            catch (Exception ex)
            {
                ExMessageBox.Show(ex.Message, AppSetter.SoftName);
            }
        }

        private void gvMat_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            GridView currentView = sender as GridView;
            Rectangle r = e.Bounds;
            if (e.Column.FieldName == "bunkerno")
            {
                object value = currentView.GetRowCellValue(e.RowHandle,
                  currentView.Columns["bunkerno"]);
                if (value != null && value.ToString() == "99")
                {
                    //Change the text to display  
                    //The e.Handled parameter is false  
                    //So the cell will be painted using the default appearance settings  
                    e.DisplayText = "人工录入";
                }
                else
                    e.DisplayText = "自动采集";
            }
        }

        private void gvWS_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            GridView currentView = sender as GridView;
            Rectangle r = e.Bounds;
            if (e.Column.FieldName == "bunkerno")
            {
                object value = currentView.GetRowCellValue(e.RowHandle,
                  currentView.Columns["bunkerno"]);
                if (value != null && value.ToString() == "99")
                {
                    //Change the text to display  
                    //The e.Handled parameter is false  
                    //So the cell will be painted using the default appearance settings  
                    e.DisplayText = "人工录入";
                }
                else
                    e.DisplayText = "自动采集";
            }
        }
    }
}
